import axios from 'axios'
import Element from 'element-ui'

//创建axios实例
const service = axios.create({
  baseURL: "http://localhost:9001", // api的base_url
  timeout: 6000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    if (sessionStorage.getItem("token")!=null||sessionStorage.getItem("token")!==''){
      config.headers["token"] = sessionStorage.getItem("token")
    }
    return config
  },
  error => {
    console.log(error)
    Promise.reject(error)
  }
)

// respone拦截器
service.interceptors.response.use(
  response => {

    const res = response.data
    if (res.code !== 200) {
      Element.Message({type:'error',message:res.msg})
      console.log(res)
      return Promise.reject('error')
    } else {
      return response.data
    }
  },
  error => {
    console.log('err' + error) // for debug
    return Promise.reject(error)
  }
)

export default service
